package com.mybatis.generate.provider.db.model;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.mybatis.generate.provider.db.DbTableFactory;
import com.mybatis.generate.util.StringHelper;

public class Table {
	private String sqlName;
	private String remarks;
	private String customClasssName;
	/** the name of the owner of the synonym if this table is a synonym */
	private String ownerSynonymName = null;
	List columns = new ArrayList();
	List primaryKeyColumns = new ArrayList();
	private Column primaryKeyColumn;
	
	private int hasDate; // 1:包含   0：不包含
	
	private int hasDecimal; // 1:包含   0：不包含
	
	public String getSqlName() {
		return sqlName;
	}
	public void setSqlName(String sqlName) {
		this.sqlName = sqlName;
	}
	public String getRemarks() {
		return remarks;
	}
	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}
	public String getClassName() {
		String defaultValue = StringHelper.makeAllWordFirstLetterUpperCase(StringHelper.toUnderscoreName(getSqlName()));
		return StringHelper.emptyIf(customClasssName, defaultValue);
	}
	public void setClassName(String customClasssName) {
		this.customClasssName = customClasssName;
	}
	public String getOwnerSynonymName() {
		return ownerSynonymName;
	}
	public void setOwnerSynonymName(String ownerSynonymName) {
		this.ownerSynonymName = ownerSynonymName;
	}
	public List getColumns() {
		return columns;
	}
	public void setColumns(List columns) {
		this.columns = columns;
	}
	public List getPrimaryKeyColumns() {
		return primaryKeyColumns;
	}
	public void setPrimaryKeyColumns(List primaryKeyColumns) {
		this.primaryKeyColumns = primaryKeyColumns;
	}
	
	public void addColumn(Column column) {
		columns.add(column);
	}
	
	public int getHasDate() {
		return hasDate;
	}
	public void setHasDate(int hasDate) {
		this.hasDate = hasDate;
	}
	public int getHasDecimal() {
		return hasDecimal;
	}
	public void setHasDecimal(int hasDecimal) {
		this.hasDecimal = hasDecimal;
	}
	public Column getPrimaryKeyColumn() {
		return primaryKeyColumn;
	}
	public void setPrimaryKeyColumn(Column primaryKeyColumn) {
		this.primaryKeyColumn = primaryKeyColumn;
	}
	
	/*public void initExportedKeys(DatabaseMetaData dbmd) {
		// TODO Auto-generated method stub
		
	}
	
	public void initImportedKeys(DatabaseMetaData dbmd) {
		 // get imported keys a
		
		   ResultSet fkeys = dbmd.getImportedKeys(catalog,schema,this.sqlName);

		   while ( fkeys.next()) {
			 String pktable = fkeys.getString(PKTABLE_NAME);
			 String pkcol   = fkeys.getString(PKCOLUMN_NAME);
			 String fktable = fkeys.getString(FKTABLE_NAME);
			 String fkcol   = fkeys.getString(FKCOLUMN_NAME);
			 String seq     = fkeys.getString(KEY_SEQ);
			 Integer iseq   = new Integer(seq);
			 getImportedKeys().addForeignKey(pktable,pkcol,fkcol,iseq);
		   }
		   fkeys.close();
	}
	
	String catalog = DbTableFactory.getInstance().getCatalog();
	String schema = DbTableFactory.getInstance().getSchema();
	private ForeignKeys exportedKeys;
	private ForeignKeys importedKeys;
	
	public    static final String PKTABLE_NAME  = "PKTABLE_NAME";
	public    static final String PKCOLUMN_NAME = "PKCOLUMN_NAME";
	public    static final String FKTABLE_NAME  = "FKTABLE_NAME";
	public    static final String FKCOLUMN_NAME = "FKCOLUMN_NAME";
	public    static final String KEY_SEQ       = "KEY_SEQ";*/
}
